﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EbayMaster
{
    public partial class FrmUserMessage : Form
    {
        public EbayTransactionType EbayTransaction = null;
        public AccountType Account = null;
        public bool SentMessage = false;

        public FrmUserMessage()
        {
            InitializeComponent();
        }

        private readonly int MessageId_ColumnIndex = 0;
        private readonly int ReceiveDate_ColumnIndex = 3;

        private void SetupTransactionMessageSubjectDataGridViewColumns()
        {
            this.dataGridViewTransactionMessageSubject.AutoGenerateColumns = false;
            this.dataGridViewTransactionMessageSubject.Columns.Clear();

            // EbayMessageId
            DataGridViewTextBoxColumn messageIdCol = new DataGridViewTextBoxColumn();
            messageIdCol.DataPropertyName = "EbayMessageId";
            messageIdCol.HeaderText = @"消息Id";
            messageIdCol.ValueType = typeof(string);
            messageIdCol.Width = 100;
            messageIdCol.Frozen = true;
            messageIdCol.Visible = false;
            this.dataGridViewTransactionMessageSubject.Columns.Add(messageIdCol);

            // Sender
            DataGridViewTextBoxColumn senderIdCol = new DataGridViewTextBoxColumn();
            senderIdCol.DataPropertyName = "Sender";
            senderIdCol.HeaderText = @"发信人";
            senderIdCol.ValueType = typeof(string);
            senderIdCol.Width = 80;
            senderIdCol.Frozen = true;
            senderIdCol.ReadOnly = true;
            this.dataGridViewTransactionMessageSubject.Columns.Add(senderIdCol);

            // RecipientUserIdCol
            DataGridViewTextBoxColumn recipientUserIdCol = new DataGridViewTextBoxColumn();
            recipientUserIdCol.DataPropertyName = "RecipientUserId";
            recipientUserIdCol.HeaderText = @"收信人";
            recipientUserIdCol.ValueType = typeof(string);
            recipientUserIdCol.Width = 80;
            recipientUserIdCol.Frozen = true;
            recipientUserIdCol.ReadOnly = true;
            this.dataGridViewTransactionMessageSubject.Columns.Add(recipientUserIdCol);

            // IsRead
            // IsReplied
            //DataGridViewCheckBoxColumn isRepliedCol = new DataGridViewCheckBoxColumn();
            //isRepliedCol.DataPropertyName = "IsReplied";
            //isRepliedCol.HeaderText = @"已回复";
            //isRepliedCol.ValueType = typeof(bool);
            //isRepliedCol.Width = 40;
            //isRepliedCol.Frozen = true;
            //this.dataGridViewTransactionMessageSubject.Columns.Add(isRepliedCol);

            // ReceiveDate
            DataGridViewTextBoxColumn receiveDateCol = new DataGridViewTextBoxColumn();
            receiveDateCol.DataPropertyName = "ReceiveDate";
            receiveDateCol.HeaderText = @"时间";
            receiveDateCol.ValueType = typeof(string);
            receiveDateCol.Width = 120;
            receiveDateCol.Frozen = true;
            receiveDateCol.ReadOnly = true;
            this.dataGridViewTransactionMessageSubject.Columns.Add(receiveDateCol);

            // Subject
            DataGridViewTextBoxColumn subjectCol = new DataGridViewTextBoxColumn();
            subjectCol.DataPropertyName = "Subject";
            subjectCol.HeaderText = @"标题";
            subjectCol.ValueType = typeof(string);
            subjectCol.Width = 450;
            subjectCol.Frozen = true;
            subjectCol.ReadOnly = true;
            this.dataGridViewTransactionMessageSubject.Columns.Add(subjectCol);

            this.dataGridViewTransactionMessageSubject.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.dataGridViewTransactionMessageSubject.MultiSelect = true;
            this.dataGridViewTransactionMessageSubject.RowPrePaint += new DataGridViewRowPrePaintEventHandler(dgv_RowPrePaint);
            this.dataGridViewTransactionMessageSubject.ScrollBars = ScrollBars.Both;
        }

        private void dgv_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            e.PaintParts &= ~DataGridViewPaintParts.Focus;
        }

        private void FrmUserMessage_Load(object sender, EventArgs e)
        {
            if (EbayTransaction == null)
                return;

            SetupTransactionMessageSubjectDataGridViewColumns();

            DataTable dtMsg = EbayMessageDAL.GetAllTransactionMessagesCompact(EbayTransaction.BuyerId, EbayTransaction.SellerName, EbayTransaction.ItemId);
            this.dataGridViewTransactionMessageSubject.DataSource = dtMsg;

            WebBrowser browser = new WebBrowser();
            this.tabPageAllMessage.Controls.Add(browser);
            browser.Dock = DockStyle.Fill;

            if (dtMsg != null && dtMsg.Rows.Count > 0)
            {
                String ebayMessageId = dtMsg.Rows[0]["EbayMessageId"].ToString();
                if (ebayMessageId == null)
                    return;

                // Display msg in web browser.
                EbayMessageType msg = EbayMessageDAL.GetOneMessage(ebayMessageId);
                if (msg == null)
                    return;

                browser.DocumentText = msg.Text;
            }
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Escape)
                this.Close();
            return base.ProcessCmdKey(ref msg, keyData);
        } 

        private void dataGridViewTransactionMessageSubject_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int colIndex = e.ColumnIndex;
            int rowIndex = e.RowIndex;

            if (rowIndex < 0 || colIndex < 0)
                return;

            String ebayMessageId = this.dataGridViewTransactionMessageSubject.Rows[rowIndex].Cells[MessageId_ColumnIndex].Value.ToString();
            if (null == ebayMessageId)
                return;

            // Display msg in web browser.
            EbayMessageType msg = EbayMessageDAL.GetOneMessage(ebayMessageId);
            if (msg == null)
                return;

            WebBrowser browser = (WebBrowser)this.tabPageAllMessage.Controls[0];
            if (browser == null)
                return;

            browser.DocumentText = msg.Text;
        }

        private void dataGridViewTransactionMessageSubject_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            dataGridViewTransactionMessageSubject_CellContentClick(sender, e);
        }

        private void buttonAskBuyer_Click(object sender, EventArgs e)
        {
            FrmAskBuyer frmAskBuyer = new FrmAskBuyer();
            frmAskBuyer.EbayTransaction = EbayTransaction;
            frmAskBuyer.Account = Account;
            frmAskBuyer.ShowDialog();

            if (frmAskBuyer.SentMessage)
            {
                FrmUserMessage_Load(sender, e);
                this.SentMessage = true;
            }
        }

        private void dataGridViewTransactionMessageSubject_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            if (e.ListChangedType == ListChangedType.ItemChanged)
                return;

            for (int rowIdx = 0; rowIdx < this.dataGridViewTransactionMessageSubject.Rows.Count; rowIdx++)
            {
                DataGridViewCell ebayMsgIdCell = this.dataGridViewTransactionMessageSubject.Rows[rowIdx].Cells[MessageId_ColumnIndex];
                if (ebayMsgIdCell.Value == null)
                    continue;

                String ebayMessageId = ebayMsgIdCell.Value.ToString();

                EbayMessageType msg = EbayMessageDAL.GetOneMessage(ebayMessageId);
                if (msg == null)
                    continue;

                if (msg.Sender == msg.SellerName)
                    this.dataGridViewTransactionMessageSubject.Rows[rowIdx].DefaultCellStyle.BackColor = ColorTranslator.FromHtml("#90EE90");
                else
                {
                    if (!msg.IsReplied)
                        this.dataGridViewTransactionMessageSubject.Rows[rowIdx].DefaultCellStyle.BackColor = ColorTranslator.FromHtml("#FFFF00");
                    else
                        this.dataGridViewTransactionMessageSubject.Rows[rowIdx].DefaultCellStyle.BackColor = ColorTranslator.FromHtml("#90EE90");
                }

                DataGridViewCell receiveDateCell = this.dataGridViewTransactionMessageSubject.Rows[rowIdx].Cells[ReceiveDate_ColumnIndex];
                if (receiveDateCell != null)
                {
                    //DateTime receiveDate = StringUtil.GetSafeDateTime(receiveDateCell.Value);
                    //receiveDate = receiveDate.ToLocalTime();
                    //receiveDateCell.Value = receiveDate;
                }
            }
        }

        private void dataGridViewTransactionMessageSubject_SelectionChanged(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection selectedRows = this.dataGridViewTransactionMessageSubject.SelectedRows;
            if (selectedRows.Count != 1)
                return;

            //String ebayMessageId = this.dataGridViewTransactionMessageSubject.Rows[rowIndex].Cells[MessageId_ColumnIndex].Value.ToString();
            String ebayMessageId = selectedRows[0].Cells[MessageId_ColumnIndex].Value.ToString();
            if (null == ebayMessageId)
                return;

            // Display msg in web browser.
            EbayMessageType msg = EbayMessageDAL.GetOneMessage(ebayMessageId);
            if (msg == null)
                return;

            if (msg.IsResponseEnabled)
                this.buttonReplyMessage.Enabled = true;
            else
                this.buttonReplyMessage.Enabled = false;
        }

        private void buttonReplyMessage_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection selectedRows = this.dataGridViewTransactionMessageSubject.SelectedRows;
            if (selectedRows.Count != 1)
                return;

            //String ebayMessageId = this.dataGridViewTransactionMessageSubject.Rows[rowIndex].Cells[MessageId_ColumnIndex].Value.ToString();
            String ebayMessageId = selectedRows[0].Cells[MessageId_ColumnIndex].Value.ToString();
            if (null == ebayMessageId)
                return;

            // Display msg in web browser.
            EbayMessageType msg = EbayMessageDAL.GetOneMessage(ebayMessageId);
            if (msg == null)
                return;

            FrmReplyBuyer frmReplyBuyer = new FrmReplyBuyer();
            frmReplyBuyer.Account = Account;
            frmReplyBuyer.EbayTransaction = EbayTransaction;
            frmReplyBuyer.EbayMessage = msg;
            frmReplyBuyer.ShowDialog();

            if (frmReplyBuyer.SentReply)
            {
                FrmUserMessage_Load(sender, e);
            }

        }  // class MessageInfoEntry
    }
}
